package studentsystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {
        ArrayList<Student> studentsList = new ArrayList<>();
       loop: while (true) {
            System.out.println("----学生管理系统----");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            System.out.println("请输入选择:");

            Scanner sc = new Scanner(System.in);

            String choose = sc.next();
            switch (choose) {
                case "1":
                   // System.out.println("添加学生");
                    addStudent(studentsList);
                    break;
                case "2":
                   deleteStudent(studentsList);
                   // System.out.println("删除学生");
                    break;
                case "3" :
                    updateStudent(studentsList);
                    //System.out.println("修改学生");
                    break;
                case "4" :
                    queryStudent(studentsList);
                    //System.out.println("查询学生");
                    break;
                case "5" :
                    System.out.println("退出");
                    break loop;
                default : System.out.println("没有这个选项");
                break;
            }
        }


    }

    public static void addStudent(ArrayList<Student> list) {
         Scanner sc = new Scanner(System.in);
        String id = null;
        while (true) {
            System.out.println("请输入学生id");
            id = sc.next();

            if (contains(list, id)){
                System.out.println("学生id已经存在，请重新录入");
            }else{
                break;
            }
            ;
        }
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        list.add(new Student(id, name, age));
        System.out.println("学生信息添加成功");
    }

    public static boolean contains(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                return true;
            }

        }
        return false;
    }

    public static void deleteStudent(ArrayList<Student> list) {
       Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生id");
        String id = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                list.remove(i);
                System.out.println("删除学生成功");
            }else{
                System.out.println("该学生id不存在，删除失败");
                break;
            }
        }

    }

    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改学生id");
        String id = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                System.out.println("请输入要修改的学生姓名");
                String name = sc.next();
                System.out.println("请输入要修改的学生年龄");
                int age = sc.nextInt();
                list.get(i).setName(name);
                list.get(i).setAge(age);
                System.out.println("修改学生信息成功");

            }else{
                System.out.println("学生id不存在");
                return;
            }
        }
    }

    public static void queryStudent(ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        //System.out.println(list);
    }

}


